package com.xl.web.controller.fns;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.xl.base.entity.Operator;
import com.xl.core.util.SessionLocalUtils;
import com.xl.core.util.SessionManager;
import com.xl.core.enums.CommonEnum;

/**
 * 按钮权限验证
 * 
 * @author 梁华
 */
public class PermissionsTag extends TagSupport {

	/** */
	private static final long serialVersionUID = 7353930247274778962L;

	// 权限标识
	private String value;

	@Override
	public int doStartTag() throws JspException {

		boolean result = false;

		// 获取到当前登录的用户
		Operator operator = SessionLocalUtils.getOperator();
		if (operator == null) {
			// 请求servlet
			HttpServletRequest request = (HttpServletRequest) pageContext
					.getRequest();
			operator = (Operator) SessionManager.get(request, CommonEnum.cookie_type.TYPE_01.getValue());
			// 存入本服务器缓存中，用于提高访问速度
			SessionLocalUtils.setOperator(operator);
		}
		// 判断权限
		if (operator != null && operator.getMember() != null
				&& operator.getMember().getPermissionsList() != null) {
			List<String> permissionsList = operator.getMember()
					.getPermissionsList();
			if (permissionsList.contains(this.value)) {
				result = true;
			}
		}

		// 真：返回EVAL_BODY_INCLUDE（执行标签）；假：返回SKIP_BODY（跳过标签不执行）
		return result ? EVAL_BODY_INCLUDE : SKIP_BODY;
	}

	// @Override
	// public int doEndTag() throws JspException {
	// return EVAL_PAGE;
	// }

	@Override
	public void release() {
		super.release();
		this.value = null;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		if (value != null) {
			this.value = value.trim();
		}
		this.value = value;
	}

}
